#!/usr/bin/env bash

# Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

if [ "${arch}" == "${host_arch}" ] && [ "${build_arch}" != "${host_arch}" ] && 
    [ ${NO_PYTHON_CROSS_SOABI} -eq 0 ]; then
    # fix cross python soabi
    sed -i "s/'EXT_SUFFIX': '.cpython-312-${arch}-linux-${triple_env}.so'/'EXT_SUFFIX': '.cpython-312-${build_arch}-linux-${build_triple_env}.so'/g" \
        $(pwd)/host/usr/lib/python3.12/_sysconfigdata__linux_${arch}-linux-${triple_env}.py

    # fix cmake find cross python
    for i in python3.12-config python3-config; do
        sed -i "s/prefix_real=\$(installed_prefix \"\$0\")/prefix_real=\"\/usr\"/g" \
            $(pwd)/host/usr/bin/${i}
        sed -i "s#LIBPL=\$(echo \"\${prefix}/lib/python3.12/config-\${VERSION}\${ABIFLAGS}-${arch}-linux-${triple_env}\" | sed \"s\#\$prefix\#\$prefix_real\#\")#LIBPL=\$(echo \"\${prefix}/lib/python3.12/config-\${VERSION}\${ABIFLAGS}-${build_arch}-linux-${build_triple_env}\" | sed \"s\#\$prefix\#\$prefix_real\#\")#g" \
            $(pwd)/host/usr/bin/${i}
    done
fi
